package cn.yuanqiao.cqbc.fileBackupLog.service.impl;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import cn.yuanqiao.common.utils.DateUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import cn.yuanqiao.cqbc.fileBackup.domain.Filebackup;
import cn.yuanqiao.cqbc.fileBackup.mapper.FilebackupMapper;
import cn.yuanqiao.cqbc.fileBackup.util.FileUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.yuanqiao.cqbc.fileBackupLog.mapper.FileBackupLogMapper;
import cn.yuanqiao.cqbc.fileBackupLog.domain.FileBackupLog;
import cn.yuanqiao.cqbc.fileBackupLog.service.IFileBackupLogService;

import javax.annotation.Resource;

/**
 * 文件备份日志Service业务层处理
 * 
 * @author xy
 * @date 2024-06-18
 */
@Service
public class FileBackupLogServiceImpl implements IFileBackupLogService 
{
    @Autowired
    private FileBackupLogMapper fileBackupLogMapper;

    @Autowired
    private FilebackupMapper filebackupMapper;

    @Resource
    private FileUtil fileUtil;
    /**
     * 查询文件备份日志
     * 
     * @param id 文件备份日志主键
     * @return 文件备份日志
     */
    @Override
    public FileBackupLog selectFileBackupLogById(Long id)
    {
        return fileBackupLogMapper.selectFileBackupLogById(id);
    }

    /**
     * 查询文件备份日志列表
     * 
     * @param fileBackupLog 文件备份日志
     * @return 文件备份日志
     */
    @Override
    public List<FileBackupLog> selectFileBackupLogList(FileBackupLog fileBackupLog)
    {
        fileBackupLog.setTenantCode(SecurityUtils.getTenantCode());
        return fileBackupLogMapper.selectFileBackupLogList(fileBackupLog);
    }

    /**
     * 新增文件备份日志
     * 
     * @param fileBackupLog 文件备份日志
     * @return 结果
     */
    @Override
    public int insertFileBackupLog(FileBackupLog fileBackupLog)
    {
        SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        fileBackupLog.setCreateTime(sdf.format(new Date()));
        return fileBackupLogMapper.insertFileBackupLog(fileBackupLog);
    }

    /**
     * 修改文件备份日志
     * 
     * @param fileBackupLog 文件备份日志
     * @return 结果
     */
    @Override
    public int updateFileBackupLog(FileBackupLog fileBackupLog)
    {
        return fileBackupLogMapper.updateFileBackupLog(fileBackupLog);
    }

    /**
     * 批量删除文件备份日志
     * 
     * @param ids 需要删除的文件备份日志主键
     * @return 结果
     */
    @Override
    public int deleteFileBackupLogByIds(Long[] ids)
    {
        return fileBackupLogMapper.deleteFileBackupLogByIds(ids);
    }

    /**
     * 删除文件备份日志信息
     * 
     * @param id 文件备份日志主键
     * @return 结果
     */
    @Override
    public int deleteFileBackupLogById(Long id)
    {
        return fileBackupLogMapper.deleteFileBackupLogById(id);
    }

    @Override
    public void restore(FileBackupLog fileBackupLog) {
        try {
            Filebackup fileBackup=filebackupMapper.selectFilebackupById(fileBackupLog.getFilebackupId());
            //开始还原
            fileUtil.copy(new File(fileBackup.getBackupAddress()), new File(fileBackup.getOriginalAddress()),fileBackup.getBackupAddress());

        }catch (Exception ex){
            ex.printStackTrace();
        }

    }
}
